//
//  RedBubbleStack.swift
//  demo01
//
//  Created by babybus on 2025/7/8.
//
import Foundation
import UIKit
import SnapKit
import Alamofire

class RedBubbleStack:UIView{
    private var bubbleLabelView:UIView!
    private var bubbleLabel:UILabel!
    private var bubbleButton:UIButton!
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    required init(coder: NSCoder) {
        fatalError("init(coder:NSCoder) -- fatalError")
    }
    func configure(img:String,bubbleText:String,title:String){
        bubbleButton.setBackgroundImage(UIImage(named: img) ?? UIImage(named: "redBubble"), for: .normal)
        bubbleButton.setTitle(bubbleText, for: .normal)
        bubbleButton.setTitleColor(UIColor(red: 219/255, green: 79/255, blue: 52/255, alpha: 1.0), for: .normal)
        bubbleButton.titleLabel?.textAlignment = .center
        
        bubbleLabel.text = title
        bubbleLabel.textAlignment = .center
    }
    //布局
    func setupUI(){
        bubbleLabelView = UIView()
        bubbleLabel = UILabel()
        bubbleButton = UIButton()
        
        bubbleButton.setBackgroundImage(UIImage(named: "redBubble"), for: .normal)
        bubbleButton.setTitle("升级会员\n全场免解锁", for: .normal)
        bubbleButton.titleLabel?.font = UIFont.systemFont(ofSize: 13)
        bubbleButton.setTitleColor(UIColor(red: 98/255, green: 11/255, blue: 0/255, alpha: 1.0), for: .normal)
        bubbleButton.titleLabel?.numberOfLines = 0
        bubbleButton.titleLabel?.textAlignment = .center
        bubbleButton.titleLabel?.lineBreakMode = .byWordWrapping
        
        let backgroundImage = UIImageView(image: UIImage(named: "bubblePrompt"))
        backgroundImage.contentMode = .scaleAspectFill // 或 .scaleToFill
        backgroundImage.clipsToBounds = true
        bubbleLabel.text = "字数最多最多"
        bubbleLabel.textColor = .white
        bubbleLabel.textAlignment = .center
        bubbleLabel.font = UIFont.systemFont(ofSize: 10)
        bubbleLabel.adjustsFontSizeToFitWidth = true
        bubbleLabel.minimumScaleFactor = 0.5
        
        bubbleLabelView.addSubview(backgroundImage)
        bubbleLabelView.addSubview(bubbleLabel)
        backgroundImage.snp.makeConstraints { make in
            make.width.height.equalToSuperview()
        }
        bubbleLabel.snp.makeConstraints { make in
            make.centerX.centerY.equalToSuperview()
            make.width.equalTo(60)
            make.height.equalTo(12)
        }
        bubbleLabelView.layer.cornerRadius = 12
        bubbleLabelView.layer.masksToBounds = true
        
        self.addSubview(bubbleButton)
        self.addSubview(bubbleLabelView)
        
        bubbleButton.snp.makeConstraints { make in
            make.width.height.equalTo(78)
        }
        bubbleLabelView.snp.makeConstraints { make in
            make.width.equalTo(72)
            make.height.equalTo(20)
            make.centerX.equalToSuperview()
            make.top.equalToSuperview().inset(58)
        }
    }

}
